#!/usr/bin/env python
from Engine.Physics_object import Physics_object
from pygame import *
from Foot import Foot
from Hand import Hand
from Weapon.Guns import *
import random
from math import pi
from Engine.Const import FPS

class Enemy(Physics_object):    
    def _init_(self, level, il, x, y, walk_speed):
        self.init(level, x, y, self.image, (8, 8, 48, 52))
        self.walk_speed = walk_speed + random.random()
        self.dir = 0
        self.ai_check_width = 128
        self.ai_shoot = False
        self.shoot_timer = FPS / 2
        self.gun = None
        if random.randint(0, 1) == 0:
            self.change_dir(0)
            self.hspeed = self.walk_speed
        else:
            self.change_dir(180)
            self.hspeed = -self.walk_speed
        
    def ai_shoot_check(self, level):
        player = level.player
        ai_found_player = False
        if (self.rect[1] <= player.rect[1] + player.rect[3] - 4 and
            self.rect[1] + self.rect[3] >= player.rect[1] + 4):
            if self.dir == 0:
                if (self.rect[0] + self.rect[2] + self.ai_check_width >= player.rect[0] and
                    self.rect[0] + self.rect[2] < player.rect[0] + player.rect[2]):
                    ai_found_player = True
            else:
                if (self.rect[0] - self.ai_check_width <= player.rect[0] + player.rect[2] and
                    self.rect[0] > player.rect[0]):
                    ai_found_player = True
        if ai_found_player:
            bak_rect = self.rect
            if self.dir == 0:
                self.rect = (self.rect[0] + self.rect[2] + 2, self.rect[1] + self.rect[3] / 2 - 4,
                             (player.rect[0] - 2)-(self.rect[0] + self.rect[2] + 2), 8)
            else:
                self.rect = (player.rect[0] + player.rect[2] + 2, self.rect[1] + self.rect[3] / 2 - 4,
                             (self.rect[0] - 2)-(player.rect[0] + player.rect[2] + 2), 8)
            regions = self.find_collision_region(level)
            coll_objs = []
            for r in regions:
                coll_objs += level.find_collision(self.rect, r, self, False, True)
            self.rect = bak_rect
            if coll_objs.__contains__(player):
                coll_objs.remove(player)
            if len(coll_objs) == 0:
                return True
        return False
                
    def override_update(self, level, events):
        ##foot controle
        if self.vspeed > 0:
            for f in self.feet:
                f.fall(self.vspeed)
        elif self.vspeed < 0:
            for f in self.feet:
                f.jump(self.vspeed)
        elif self.vspeed == 0 and not self.collbot:
            for f in self.feet:
                f.fall(self.vspeed)
        elif self.vspeed == 0 and self.collbot and (self.perform_friction or self.hspeed == 0):
            for f in self.feet:
                f.stopWalking()
        elif self.vspeed == 0 and self.collbot and not self.perform_friction:
            for f in self.feet:
                f.walk(self.hspeed)
        
        ##hand controle
        if self.vspeed > 0:
            for h in self.hands:
                h.fall()
        elif self.vspeed == 0 and self.collbot and not self.perform_friction:
            for h in self.hands:
                h.walk(self.hspeed)
        else:
            for h in self.hands:
                h.stopWalking()
      
        if self.collbot and self.ai_shoot == False:
            if self.dir == 0:
                if self.coll_obj_bot.ai_end_right:
                    if self.rect[0] + self.rect[2] > self.coll_obj_bot.rect[0] + self.coll_obj_bot.rect[2] + self.rect[2]/2:
                        self.change_dir(180)
                        self.hspeed = -self.walk_speed
            else:
                if self.coll_obj_bot.ai_end_left:
                    if self.rect[0] < self.coll_obj_bot.rect[0] - self.rect[2]/2:
                        self.change_dir(0)
                        self.hspeed = self.walk_speed
            if self.hspeed == 0:
                if self.dir == 0:
                    self.change_dir(180)
                    self.hspeed = -self.walk_speed
                else:
                    self.change_dir(0)
                    self.hspeed = self.walk_speed
            else:
                if self.dir == 0:
                    self.hspeed = self.walk_speed
                else:
                    self.hspeed = -self.walk_speed
        else:
            self.hspeed = 0
        
        ##----AI----##
        if self.ai_shoot_check(level):
            self.hspeed = 0
            self.ai_shoot = True
        else:
            if self.ai_shoot:
                self.ai_shoot = False
                if self.dir == 0:
                    self.hspeed = self.walk_speed
                else:
                    self.hspeed = -self.walk_speed
        if self.ai_shoot:
            self.shoot(level)
        else:
            self.shoot_timer = FPS / 2
    
    def shoot(self, level):
        if self.hands[0].gun != None:
            self.shoot_timer += 1
            if self.shoot_timer >= FPS / 2:
                self.shoot_timer = 0
                if self.dir == 0:
                    self.hands[0].shoot(level, self.x, self.y, 20, self.dir * pi/180)
                else:
                    self.hands[0].shoot(level, self.x - 96, self.y, 20, self.dir * pi/180)
    
    def change_dir(self, dir):
        if self.dir != dir:
            self.dir = dir
            self.image = transform.flip(self.image, True, 0)
            for h in self.hands:
                h.set_direction(dir)
            for f in self.feet:
                f.set_direction(dir)
                
    def override_back_draw(self, camera):
        self.hands[1].draw(camera, self.x, self.y)
        self.feet[1].draw(camera, self)
            
    def override_front_draw(self, camera):
        self.feet[0].draw(camera, self)
        self.hands[0].draw(camera, self.x, self.y)
 
class Enemy1(Enemy):
    def __init__(self, level, il, x, y):       
        #feet related
        self.feet = []
        self.feet.append(Foot(8, 54, il.enemy_feet[0][0], 1))
        self.feet.append(Foot(8, 50, il.enemy_feet[0][1], 2))      
        
        #hands related
        self.hands = []
        self.hands.append(Hand(il.enemy_hands[0][0], 1, 22, 42))
        self.hands.append(Hand(il.enemy_hands[0][1], 2, 22, 36))
        
        #gun
        self.gun = Magnum(il)
        self.hands[0].switchGun(self.gun)
        
        self.image = il.enemy_body[0]
        self._init_(level, il, x, y, 3)
    
    def bullet_hit(self, hit_pos):
        for i in range(0, 10):
            self.level.create_blood(hit_pos[0], hit_pos[1])
    
    def event_death(self):
        for i in range(0, 50):
            self.level.create_blood(self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2)
    
    def get_new_instance(self, level, il):
        return Enemy1(level, il, 0, 0)
    
class Enemy2(Enemy):
    def __init__(self, level, il, x, y):       
        #feet related
        self.feet = []
        self.feet.append(Foot(8, 56, il.enemy_feet[1][0], 1))
        self.feet.append(Foot(8, 54, il.enemy_feet[1][1], 2))
        
        #gun
        self.gun = None
        
        #hands related
        self.hands = []
        self.hands.append(Hand(il.enemy_hands[1][0], 1, 22, 42))
        self.hands.append(Hand(il.enemy_hands[1][1], 2, 22, 36))
               
        self.image = il.enemy_body[1]
        self._init_(level, il, x, y, 2.5)
    
    def bullet_hit(self, hit_pos):
        for i in range(0, 10):
            self.level.create_blood(hit_pos[0], hit_pos[1])
    
    def event_death(self):
        for i in range(0, 50):
            self.level.create_blood(self.rect[0] + self.rect[2]/2, self.rect[1] + self.rect[3]/2)
        
    def get_new_instance(self, level, il):
        return Enemy2(level, il, 0, 0)